public class IntegerListArray {
protected Integer[] theArray;
protected int listSize;

public IntegerListArray() {
	theArray = new Integer[5];
	listSize=0;
}

public void ensureCapacity(int requestedSize) {
    if (requestedSize>=theArray.length) {
        Integer[] newArray =  new Integer[theArray.length*2];
        System.arraycopy(theArray, 0, newArray, 0, theArray.length);
        theArray = newArray;
    }
}

public void add(Integer elt) {
    ensureCapacity(listSize+1);
    theArray[listSize++] = elt;
    }


public void addFirst(Integer elt) {
	addAt(elt, 0);
}

public int find(Integer elt) {
	int idx = -1;
	for (int i=0; i<listSize; i++) {
		if (elt.equals(theArray[i]))
			idx=i;
	}
	return idx;
}

public Integer findKth(int idx){
    if (idx<0 || idx>=listSize)
        return null;
    return theArray[idx];
}

public void addAt(Integer elt, int idx) {
    if ((idx>listSize) || (idx<0))
        throw new IllegalArgumentException("Illegal insertion point " + idx);
    if (idx==listSize) ensureCapacity(listSize+1);
    makeRoomAt(idx);
    theArray[idx] = elt;
}
public boolean isEmpty() {
	return (listSize==0);
}

public void makeEmpty(){
	theArray = new Integer[5];
	listSize=0;
}
public void makeRoomAt(int idx) {
	ensureCapacity(listSize+1);
    //NB: listSize is incremented after initialization
    for (int i=listSize++; i>idx; i--)
        theArray[i]=theArray[i-1];
}

public void printList() {
    for (int i=0; i<listSize; i++)
    System.out.print(theArray[i]+ ((i+1<listSize)?", ":"\n"));
}

public boolean removeAt(int idx){
	if (idx<0 || idx>=listSize)
		return false;
	fillInHoleAt(idx);
	return true;
}

public void fillInHoleAt(int idx) {
	for (int i = idx; i<listSize; i++)
	    theArray[i] = theArray[i+1];
	--listSize;

}

public boolean remove(Integer idx){
	int remloc = find(idx);
	if (remloc == -1)
		return false;
	removeAt(remloc);
	return true;
}
}